#include "ExportPlist.h"
#include <iostream>

using namespace std;


ExportPlist::ExportPlist()
{
	this->spriteName = NULL;
	this->sprite = NULL;
	this->plistType = 0;
}

ExportPlist::~ExportPlist()
{
	if ( this->spriteName )
		delete this->spriteName;
}

void ExportPlist::setSpriteName ( char * spriteName )
{
	if ( this->spriteName )
		delete this->spriteName;
	if ( this->sprite )
		delete this->sprite;

	this->spriteName = NULL;
	this->sprite = NULL;

	this->spriteName = new char[strlen ( spriteName )];
	strcpy ( this->spriteName, spriteName );

	sprite = Performance::getInstant()->LoadSprite( this->spriteName );
}

void ExportPlist::setPlistType ( int plistType )
{
	this->plistType = plistType;
}

//out put 
char* ExportPlist::getOutFile()
{
	char* imageName = Util::getInstant()->getImageName(this->spriteName);
	char* outFile = new char [ strlen (imageName) + strlen ( ".plist" )];
	strcpy ( outFile, imageName );
	strcat ( outFile, ".plist" );	
	return outFile;
}

void ExportPlist::begin()
{
	FILE* fout = fopen ( getOutFile(), "w" );

	fprintf ( fout, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
	fprintf ( fout, "\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" );
	fprintf ( fout, "\n<plist version=\"1.0\">" );
	fprintf ( fout, "\n<dict>" );

	fclose ( fout );
}

void ExportPlist::end()
{
	FILE* fout = fopen ( getOutFile(), "a" );

	fprintf ( fout, "\n</dict>" );	
	fprintf ( fout, "\n</plist>" );

	fclose ( fout );
}

void ExportPlist::exportTexture()
{
	FILE* fout = fopen ( getOutFile(), "a" );

	fprintf ( fout, "\n\t<key>texture</key>" );	
	fprintf ( fout, "\n\t<dict>" );
	fprintf ( fout, "\n\t\t<key>width</key>" );
	fprintf ( fout, "\n\t\t<integer>%d</integer>", sprite->imageSize.w );
	fprintf ( fout, "\n\t\t<key>height</key>" );
	fprintf ( fout, "\n\t\t<integer>%d</integer>", sprite->imageSize.h );
	fprintf ( fout, "\n\t</dict>" );

	fclose ( fout );
}

void ExportPlist::exportFrame()
{
	FILE* fout = fopen ( getOutFile(), "a" );

	fprintf ( fout, "\n\t<key>frames</key>" );	
	fprintf ( fout, "\n\t<dict>" );

	//frame by module of sprite
	for ( int i = 0; i < this->sprite->numOfModule; i++ )
	{
		if ( sprite->module[i].moduleSize.x == -1 && sprite->module[i].moduleSize.y == -1 && sprite->module[i].moduleSize.w == -1 && sprite->module[i].moduleSize.h == -1 )
			continue;
		fprintf ( fout, "\n\t\t<key>%s%s</key>", sprite->module[i].moduleName, ".png" );	
		fprintf ( fout, "\n\t\t<dict>" );
		fprintf ( fout, "\n\t\t\t<key>x</key>" );
		fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[i].moduleSize.x );
		fprintf ( fout, "\n\t\t\t<key>y</key>" );
		fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[i].moduleSize.y );
		fprintf ( fout, "\n\t\t\t<key>width</key>" );
		fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[i].moduleSize.w );
		fprintf ( fout, "\n\t\t\t<key>height</key>" );
		fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[i].moduleSize.h );
		fprintf ( fout, "\n\t\t\t<key>offsetX</key>" );
		fprintf ( fout, "\n\t\t\t<real>0</real>" );
		fprintf ( fout, "\n\t\t\t<key>offsetY</key>" );
		fprintf ( fout, "\n\t\t\t<real>0</real>" );
		fprintf ( fout, "\n\t\t\t<key>originalWidth</key>" );
		fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[i].moduleSize.w );
		fprintf ( fout, "\n\t\t\t<key>originalHeight</key>" );
		fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[i].moduleSize.h );
		fprintf ( fout, "\n\t\t</dict>" );
	}

	//frame of anim 
	for ( int i = 0; i < this->sprite->numOfAnim; i++ )
	{
		for ( int j = 0; j < sprite->anim[i].numAframe; j++ )
		{

			int moduleID = sprite->getModuleID ( sprite->anim[i].aFrameID[j].fModuleID );

			fprintf ( fout, "\n\t\t<key>%s_%d%s</key>", sprite->anim[i].animName, j, ".png" );	
			fprintf ( fout, "\n\t\t<dict>" );
			fprintf ( fout, "\n\t\t\t<key>x</key>" );
			fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[moduleID].moduleSize.x );
			fprintf ( fout, "\n\t\t\t<key>y</key>" );
			fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[moduleID].moduleSize.y );
			fprintf ( fout, "\n\t\t\t<key>width</key>" );
			fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[moduleID].moduleSize.w );
			fprintf ( fout, "\n\t\t\t<key>height</key>" );
			fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[moduleID].moduleSize.h );
			fprintf ( fout, "\n\t\t\t<key>offsetX</key>" );
			fprintf ( fout, "\n\t\t\t<real>%d</real>", sprite->anim[i].aFrameID[j].position.x );
			fprintf ( fout, "\n\t\t\t<key>offsetY</key>" );
			fprintf ( fout, "\n\t\t\t<real>%d</real>", -sprite->anim[i].aFrameID[j].position.y );
			fprintf ( fout, "\n\t\t\t<key>originalWidth</key>" );
			fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[moduleID].moduleSize.w );
			fprintf ( fout, "\n\t\t\t<key>originalHeight</key>" );
			fprintf ( fout, "\n\t\t\t<integer>%d</integer>", sprite->module[moduleID].moduleSize.h );
			fprintf ( fout, "\n\t\t</dict>" );
		}
	}
	
	fprintf ( fout, "\n\t</dict>" );

	fclose ( fout );
}

void ExportPlist::exportPlist()
{
	begin();
	if ( plistType == PLIST_FRAME )
	{
		exportTexture();
		exportFrame();
	}
	end();
}